/*
    Copyright 1996-2008 Ariba, Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

    $Id: //ariba/platform/util/core/ariba/util/core/VersionComponentChecksum.java#5 $
*/

package ariba.util.core;

import ariba.util.log.Log;
import java.io.UnsupportedEncodingException;

/**
    Component checksum for the application version.

    @aribaapi ariba
*/
public class VersionComponentChecksum
    extends SimpleComponentChecksum
{
        // ToDo: should be extended to add hotfix information

    /*-----------------------------------------------------------------------
        Private Class Fields
      -----------------------------------------------------------------------*/

    /** Static state for the class. */
    private static State STATE;

    /*-----------------------------------------------------------------------
        Constructors
      -----------------------------------------------------------------------*/

    /**
        Creates a new VersionComponentChecksum with the given name.

        @param name name of checksum
        @return new VersionComponentChecksum
    */
    private VersionComponentChecksum (String name, byte[] checksum)
    {
        super(name, checksum);
    }

    /*-----------------------------------------------------------------------
        Public Methods
      -----------------------------------------------------------------------*/

    /**
        Returns the static instance of VersionComponentChecksum (the only one
        that is ever needed).

        @aribaapi ariba

        @return instance of VersionComponentChecksum
    */
    public static VersionComponentChecksum getVersionComponentChecksum ()
    {
        VersionComponentChecksum versionComponentChecksum =
            (VersionComponentChecksum)getState().get();
        if (versionComponentChecksum == null) {
            synchronized (VersionComponentChecksum.class) {
                versionComponentChecksum = (VersionComponentChecksum)getState().get();
                if (versionComponentChecksum == null) {
                    byte[] checksum;
                    try {
                        checksum = Version.versionImage.getBytes(MIME.CharSetUTF);
                    }
                    catch (UnsupportedEncodingException e) {
                            // Should never happen; UTF-8 should be supported
                            // "Unable to get application version checksum: {0}"
                        Log.util.error(5245, e);
                        checksum = new byte[0];
                    }
                    versionComponentChecksum =
                        new VersionComponentChecksum(
                            Version.class.getName(), checksum);
                    getState().set(versionComponentChecksum);
                }
            }
        }
        return versionComponentChecksum;
    }

    /*-----------------------------------------------------------------------
        Private Methods
      -----------------------------------------------------------------------*/

    /**
        Gets the state.

        @return state
    */
    private static State getState ()
    {
        if (STATE == null) {
            synchronized (VersionComponentChecksum.class) {
                if (STATE == null) {
                    STATE = new GlobalState();
                }
            }
        }
        return STATE;
    }

    /**
        Sets the state.

        @param state state
    */
    private static void setState (State state)
    {
        synchronized (VersionComponentChecksum.class) {
            STATE = state;
        }
    }

    /**
        Sets the static instance of VersionComponentChecksum.

        @param versionComponentChecksum the instance
    */
    private static void setVersionComponentChecksum (
        VersionComponentChecksum versionComponentChecksum
    )
    {
        synchronized (VersionComponentChecksum.class) {
            getState().set(versionComponentChecksum);
        }
    }
}
